package com.bl.student.service.biz.art.impl;

import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.enums.ArtExamType;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.ArtExamEntity;
import com.bl.student.dao.repository.ArtExamRespository;
import com.bl.student.dao.repository.StudioRespository;
import com.bl.student.service.biz.art.ArtExamService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.DelListBody;
import com.bl.student.service.vo.art.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liangweikang on 18/1/29.
 */
@Service
@Log4j2
public class ArtExamServiceImpl implements ArtExamService{

    @Autowired
    ArtExamRespository artExamRespository;

    @Autowired
    StudioRespository studioRespository;

    @Override
    public ArtExamListVo getArtExamList(ArtExamType artExamType, Pageable pageable) {
        ArtExamListVo artExamListVo = new ArtExamListVo();

        Page<ArtExamEntity> artExamEntities;

        artExamEntities = artExamRespository.findByTypeOrderByWeightDesc(artExamType,pageable);

        artExamListVo.setTotalCount(artExamEntities.getTotalElements());
        artExamListVo.setList(artExamEntities.getContent());

        return artExamListVo;
    }

    @Override
    public void del(DelBody id) {
        artExamRespository.delete(id.getId());
    }

    @Override
    public void delLunbo(String id) {

    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            artExamRespository.delete(id);
        }
    }

    @Override
    public ArtExamEntity getDetail(String id) {
        return artExamRespository.findOne(id);
    }

    @Override
    public void modify(ArtExamModVo artExamModVo) {


        ArtExamEntity artExamEntity = artExamRespository.findOne(artExamModVo.getId());

        if (artExamEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应艺考信息");
        }
        artExamEntity.setCover(artExamModVo.getCover());
        artExamEntity.setDescription(artExamModVo.getDescription());
        artExamEntity.setDetailId(artExamModVo.getDetailId());
        artExamEntity.setId(artExamModVo.getId());
        artExamEntity.setStatus(artExamModVo.getStatus());
        artExamEntity.setWeight(artExamModVo.getWeight());
        artExamEntity.setUpdateTime(new Date());
        artExamEntity.setType(artExamModVo.getType());
        artExamEntity.setTitle(artExamModVo.getTitle());
        artExamEntity.setTags(artExamModVo.getTags());
        artExamRespository.save(artExamEntity);

    }

    @Override
    public void add(ArtExamAddVo artExamAddVo) {
        ArtExamEntity artExamEntity = new ArtExamEntity();
        artExamEntity.setCover(artExamAddVo.getCover());
        artExamEntity.setDescription(artExamAddVo.getDescription());
        artExamEntity.setDetailId(artExamAddVo.getDetailId());
        artExamEntity.setId(UUIDUtils.getUUID());
        artExamEntity.setStatus(artExamAddVo.getStatus());
        artExamEntity.setWeight(artExamAddVo.getWeight());
        artExamEntity.setUpdateTime(new Date());
        artExamEntity.setCreateTime(new Date());
        artExamEntity.setType(artExamAddVo.getType());
        artExamEntity.setTitle(artExamAddVo.getTitle());
        artExamEntity.setTags(artExamAddVo.getTags());
        artExamRespository.save(artExamEntity);
    }

    @Override
    public ArtExamManagerListVo getArtExameManagerListVo(ArtExamType artExamType, Pageable pageable) {
        ArtExamManagerListVo artExamManagerListVo = new ArtExamManagerListVo();

        Page<ArtExamEntity> artExamEntities;

        List<ArtExamManagerVo> artExamManagerVoList = new ArrayList<>();

        artExamEntities = artExamRespository.findByTypeOrderByCreateTimeDesc(artExamType,pageable);

        for (ArtExamEntity artExamEntity : artExamEntities) {
            ArtExamManagerVo artExamManagerVo = new ArtExamManagerVo();
            artExamManagerVo.setDetailId(artExamEntity.getDetailId());
            artExamManagerVo.setType(artExamEntity.getType());
            artExamManagerVo.setId(artExamEntity.getId());
            artExamManagerVo.setTags(artExamEntity.getTags());
            artExamManagerVo.setDescription(artExamEntity.getDescription());
            artExamManagerVo.setCover(artExamEntity.getCover());
            artExamManagerVo.setCreateTime(artExamEntity.getCreateTime());
            artExamManagerVo.setStatus(artExamEntity.getStatus());
            artExamManagerVo.setTitle(artExamEntity.getTitle());
            artExamManagerVo.setUpdateTime(artExamEntity.getUpdateTime());
            artExamManagerVo.setWeight(artExamEntity.getWeight());
            artExamManagerVo.setCount((studioRespository.countByBizId(artExamEntity.getId())));

            artExamManagerVoList.add(artExamManagerVo);
        }

        artExamManagerListVo.setTotalCount(artExamEntities.getTotalElements());
        artExamManagerListVo.setList(artExamManagerVoList);

        return artExamManagerListVo;
    }
}
